package tech.aistar.prj.controller;

import tech.aistar.prj.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/16 10:14
 */
@WebServlet(urlPatterns = "/user/loginView")
public class LoginViewAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 设置文档的输出类型
        resp.setContentType("text/html;charset=utf-8");
        //2. 获取文档输出流
        PrintWriter out = resp.getWriter();

        String error= (String) req.getAttribute("error");
        String uname=(String) req.getAttribute("username");
//        System.out.println(error);
        uname=uname==null?"":uname;
        String pwd= (String) req.getAttribute("password");
        pwd=pwd==null?"":pwd;

        //定义一个标记位(判断是否勾住)
        boolean flag=false;

        if("0".equals(error)){
            uname="";
            pwd="";
        }
        else if("1".equals(error)){
            pwd="";
        }else{
            Cookie[] cookies=req.getCookies();
            if(null!=cookies&&cookies.length>0){//清除缓存
                for (Cookie cookie : cookies) {
                    //String getName();//获取cookie对象的name属性的值
                    if("loginInfo".equals(cookie.getName())) {
                        //获取cookie中的value
                        String value = cookie.getValue();
                        if (null != value && value.trim().length() > 0) {
                            String[] arr = value.split(":");

                            //二级编码
                            arr[0] = URLDecoder.decode(arr[0], "utf-8");
                            arr[0] = URLDecoder.decode(arr[0], "utf-8");
                            uname=arr[0];
                            pwd=arr[1];
                            flag=true;
                        }
                        break;
                    }
                }
            }
        }

        out.write("<html>\n");
        out.write("<head>\n");
        out.write("    <title>登录入口</title>\n");
        out.write("    <meta charset=\"UTF-8\">\n");
        out.write("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        out.write("    <link rel=\"stylesheet\" href=\"/lk/plugins/bootstrap-3.3.7-dist/css/bootstrap.css\">\n");
        out.write("    <link rel=\"stylesheet\" href=\"/lk/css/login.css\">\n");
        out.write("</head>\n");
        out.write("<body>\n");

//        out.println("<input type='hidden' id='path' value='"+req.getServletContext().getAttribute("path")+"'>");

        out.write("    <div class=\"container-fluid\">\n");
        out.write("        <div class=\"row\">\n");
        out.write("            <nav class=\"navbar navbar-default\" role=\"navigation\">\n");
        out.write("                <div class=\"container-fluid\">\n");
        out.write("                    <div class=\"navbar-header\">\n");
        out.write("                        <button type=\"button\" class=\"navbar-toggle\" data-toggle=\"collapse\"\n");
        out.write("                                data-target=\"#example-navbar-collapse\">\n");
        out.write("                            <span class=\"sr-only\">切换导航</span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                            <span class=\"icon-bar\"></span>\n");
        out.write("                        </button>\n");
        out.write("                        <a class=\"navbar-brand\" href=\"#\">宠物网站</a>\n");
        out.write("                    </div>\n");
        out.write("                    <div class=\"collapse navbar-collapse\" id=\"example-navbar-collapse\">\n");
        out.write("                        <ul class=\"nav navbar-nav\">\n");
        out.write("                            <li><a href='/lk/pet/indexData'>首页</a></li>\n");
        out.write("                            <li class=\"active\"><a href='/lk/user/loginView'>登录</a></li>\n");
        out.write("                            <li><a href=\"#\">注册</a></li>\n");

        //只有登录之后，才会显示
        //只要登录成功，user就放入到session中
        User user= (User) req.getSession().getAttribute("user");
        if(null!=user){
            out.write("                            <li><a href=\"#\">安全退出</a></li>\n");
            out.write("                            <li><a href=\"#\">我的关注</a></li>\n");
        }

        out.write("                        </ul>\n");
        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("            </nav>\n");
        out.write("        </div>\n");
        out.write("    </div>\n");
        out.write("\n");
        out.write("    <div class=\"container login_div\">\n");
        out.write("        <div class=\"row\">\n");

        String path= (String) req.getServletContext().getAttribute("path");

        out.write("            <form action='/lk/user/logincl' method='post' class=\"form-horizontal\" role=\"form\">\n");
        out.write("                <div class=\"form-group\">\n");
        out.write("                    <label for=\"firstname\" class=\"col-sm-2 control-label\">用户名</label>\n");
        out.write("                    <div class=\"col-md-6\">\n");
        out.write("                        <input value='"+uname+"' type=\"text\" class=\"form-control\" name='username' id=\"firstname\" placeholder=\"请输入名字\">\n");

        if("0".equals(error)) {
            out.println("                         <label class=\"text-danger\">用户名不存在</label>");
        }

        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("                <div class=\"form-group\">\n");
        out.write("                    <label for=\"lastname\" class=\"col-sm-2 control-label\">密码</label>\n");
        out.write("                    <div class=\"col-md-6\">\n");
        out.write("                        <input type=\"password\" value='"+pwd+"' name='password' class=\"form-control\" id=\"lastname\" placeholder=\"请输入姓\">\n");

        if("1".equals(error)) {
            out.println("                         <label class=\"text-danger\">密码错误</label>");
        }
        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("                <div class=\"form-group\">\n");
        out.write("                    <div class=\"col-sm-offset-2 col-sm-10\">\n");
        out.write("                        <div class=\"checkbox\">\n");
        out.write("                            <label>\n");

        if(flag){
            out.write("                                <input checked name='flag' type=\"checkbox\">请记住我\n");
        }else{
            out.write("                                <input name='flag' type=\"checkbox\">请记住我\n");
        }


        out.write("                            </label>\n");
        out.write("                        </div>\n");
        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("                <div class=\"form-group\">\n");
        out.write("                    <div class=\"col-sm-offset-2 col-sm-10\">\n");
        out.write("                        <button type=\"submit\" class=\"btn btn-default\">登录</button>\n");
        out.write("                    </div>\n");
        out.write("                </div>\n");
        out.write("            </form>\n");
        out.write("        </div>\n");
        out.write("    </div>\n");
        out.write("\n");
        out.write("    <script src=\"/lk/plugins/jquery/jquery.min.js\"></script>\n");
        out.write("    <script src=\"/lk/plugins/bootstrap-3.3.7-dist/js/bootstrap.js\"></script>\n");
        out.write("</body>\n");
        out.write("</html>\n");
    }
}
